//
//  TTDianpuTemplateViewController.m
//  TaoTuo
//
//  Created by admin on 2019/11/9.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuTemplateViewController.h"
#import "TTDianpuTemplateCollectionViewCell.h"

static NSString *cellID = @"template_cell_id";

@interface TTDianpuTemplateViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, TTDianpuTemplateCollectionViewCellDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, copy) NSString *templateID;
@property (nonatomic, copy) void (^selectedTemplateIDBlock)(NSString *templateID, NSString *templateName);

@end

@implementation TTDianpuTemplateViewController

- (instancetype)initWithTemplateID:(NSString *)templateID selectedTemplateIDBlock:(nonnull void (^)(NSString * _Nonnull, NSString * _Nonnull))selectedTemplateIDBlock
{
    self = [super init];
    if (self) {
        self.templateID = templateID;
        self.selectedTemplateIDBlock = selectedTemplateIDBlock;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"模板设置";
    [self getTemplateDatas];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTDianpuTemplateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

#pragma mark - TTDianpuTemplateCollectionViewCellDelegate
- (void)selectedItem:(TTDianpuTemplateCollectionViewCell *)cell model:(TTDianpuTempleteModel *)model
{
    for (TTDianpuTempleteModel *tm in self.dataArray) {
        if (tm.TemplateValue.intValue != model.TemplateValue.intValue) {
            tm.isSelect = NO;
        }
    }
    
    [self.collectionView reloadData];
    if (self.selectedTemplateIDBlock) {
        self.selectedTemplateIDBlock(model.TemplateValue, model.TemplateName);
    }
}

- (void)getTemplateDatas
{
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:getShopStyleTemplateList_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        [self.dataArray removeAllObjects];
        for (NSDictionary *dic in responseObject) {
            TTDianpuTempleteModel *model = [TTDianpuTempleteModel modelWithJSON:dic];
            if (model.TemplateValue.intValue == self.templateID.intValue) {
                model.isSelect = YES;
            }
            [self.dataArray addObject:model];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        CGFloat w = 150;
        CGFloat space = (self.view.width - 300) / 3;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(w, 267);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = space;
        layout.sectionInset = UIEdgeInsetsMake(10, space, 10, space);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = UIColorHex(F1F5F6);
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([TTDianpuTemplateCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:cellID];
        [self.view addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_topLayoutGuideBottom);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _collectionView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


@end
